const mongoose = require('mongoose')
const BillSource = mongoose.model('billSource')
const { formatBody } = require('../../tools')

module.exports = {
  // 广告列表
  async adList (ctx) {
    let list = await BillSource.find({})
    .sort({ 'meta.createdAt': -1 })
    return ctx.body = formatBody(1, 'ok', list)
  },
  // 广告详情
  async adDetail (ctx) {
    let { _id } = ctx.request.body
    if (!_id) {
      return ctx.body = formatBody(0, '_id不可为空')
    }
    let data = await BillSource.findOne({
      _id
    }).populate({
      path: 'creator',
      select: 'avatar nickname _id'
    })
    return ctx.body = formatBody(1, 'ok', data)
  },
  async updateState (ctx) {
    let { _id, state } = ctx.request.body
    if (!_id || !state) {
      return ctx.body = formatBody(0, '_id,state不可为空')
    }
    await BillSource.update({
      _id
    }, {
      $set: { state }
    })
    return ctx.body = formatBody(1, '操作成功')
  }
}